<?php

class FRM_Tools_Mail
{
	/**
	 * Имя почтового ящика
	 *
	 * @var string
	 */
	var $smtp_username;
	
	/**
	 * Порт
	 *
	 * @var int
	 */
	var $smtp_port;
	
	/**
	 * Сервер исходящей почты
	 *
	 * @var string
	 */
	var $smtp_host;
	
	/**
	 * Пароль
	 *
	 * @var string
	 */
	var $smtp_password;
	
	/**
	 * Чтобы видеть сообщения об ощибках
	 *
	 * @var bool
	 */
	var $smtp_debug;
	
	/**
	 * Кодировка
	 *
	 * @var string
	 */
	var $smtp_charset;
	
	/**
	 * Отправитель
	 *
	 * @var string
	 */
	var $smtp_from;
	
	/**
	 * Заголовки
	 *
	 * @var unknown_type
	 */
	var $headers;
	
	function __construct(
		$smtp_username = SMTP_USERNAME, 
		$smtp_port = SMTP_PORT, 
		$smtp_host = SMTP_HOST, 
		$smtp_password = SMTP_PASSWORD, 
		$smtp_debug = SMTP_DEBUG, 
		$smtp_charset = SMTP_CHARSET, 
		$smtp_from = SMTP_FROM
	)
	{
		$this->smtp_username = $smtp_username;
		$this->smtp_port = $smtp_port;
		$this->smtp_host = $smtp_host;
		$this->smtp_password = $smtp_password;
		$this->smtp_debug = $smtp_debug;
		$this->smtp_charset = $smtp_charset;
		$this->smtp_from = $smtp_from;
		$this->headers = '';
	}
	
	function smtpMail($mail_to, $subject, $message)
	{
        $SEND = "Date: ".date("D, d M Y H:i:s") . " UT\r\n";
        $SEND .= 'Subject: =?'.$this->smtp_charset.'?B?'.base64_encode($subject)."=?=\r\n";
        if ($this->headers) $SEND .= $this->headers."\r\n\r\n";
        else
        {
            $SEND .= "Reply-To: ".$this->smtp_username."\r\n";
            $SEND .= "MIME-Version: 1.0\r\n";
            $SEND .= "Content-Type: text/plain; charset=\"".$this->smtp_charset."\"\r\n";
            $SEND .= "Content-Transfer-Encoding: 8bit\r\n";
            $SEND .= "From: \"".$this->smtp_from."\" <".$this->smtp_username.">\r\n";
            $SEND .= "To: $mail_to <$mail_to>\r\n";
            $SEND .= "X-Priority: 3\r\n\r\n";
        }
        $SEND .=  $message."\r\n";
        if (!$socket = fsockopen($this->smtp_host, $this->smtp_port, $errno, $errstr, 30) )
        {
            if ($this->smtp_debug) echo $errno."&lt;br&gt;".$errstr;
            	return false;
        }

        if (!$this->serverParse($socket, "220", __LINE__)) return false;

        fputs($socket, "HELO " . $this->smtp_host . "\r\n");
        if (!$this->serverParse($socket, "250", __LINE__))
        {
           if ($this->smtp_debug) echo '<p>Can\'t sent HELO!</p>';
           fclose($socket);
           return false;
        }
        fputs($socket, "AUTH LOGIN\r\n");
        if (!$this->serverParse($socket, "334", __LINE__))
        {
           if ($this->smtp_debug) echo '<p>Can\'t find response to authorization request.</p>';
           fclose($socket);
           return false;
        }
        fputs($socket, base64_encode($this->smtp_username) . "\r\n");
        if (!$this->serverParse($socket, "334", __LINE__))
        {
           if ($this->smtp_debug) echo '<p>Login incorrect!</p>';
           fclose($socket);
           return false;
        }
        fputs($socket, base64_encode($this->smtp_password) . "\r\n");
        if (!$this->serverParse($socket, "235", __LINE__))
        {
           if ($this->smtp_debug) echo '<p>Password incorrect!</p>';
           fclose($socket);
           return false;
        }
        fputs($socket, "MAIL FROM: <".$this->smtp_username.">\r\n");
        if (!$this->serverParse($socket, "250", __LINE__))
        {
           if ($this->smtp_debug) echo '<p>Can\'t send comand MAIL FROM: </p>';
           fclose($socket);
           return false;
        }
        fputs($socket, "RCPT TO: <" . $mail_to . ">\r\n");

        if (!$this->serverParse($socket, "250", __LINE__))
        {
           if ($this->smtp_debug) echo '<p>Can\'t send comand RCPT TO: </p>';
           fclose($socket);
           return false;
        }
        fputs($socket, "DATA\r\n");

        if (!$this->serverParse($socket, "354", __LINE__))
        {
           if ($this->smtp_debug) echo '<p>Can\'t send comand DATA</p>';
           fclose($socket);
           return false;
        }
        fputs($socket, $SEND."\r\n.\r\n");

        if (!$this->serverParse($socket, "250", __LINE__))
        {
           if ($this->smtp_debug) echo '<p>Can\'t send body!</p>';
           fclose($socket);
           return false;
        }
        fputs($socket, "QUIT\r\n");
        fclose($socket);
        return TRUE;
	}
	
	function serverParse($socket, $response, $line = __LINE__)
	{
	    while (substr(@$server_response, 3, 1) != ' ')
	    {
	        if (!(@$server_response = fgets($socket, 256)))
	        {
               if ($this->smtp_debug) echo "<p>Problems with sending mail!</p>$response<br>$line<br>";
               return false;
            }
	    }
	    if (!(substr($server_response, 0, 3) == $response))
	    {
           if ($this->smtp_debug) echo "<p>Problems with sending mail!</p>$response<br>$line<br>";
           return false;
        }
	    return true;
	}
	
	function setHeaders($mail_to, $type='html')
	{
		$header = "To: \"Administrator\" <$mail_to>\r\n";
        $header .= "From: \"$this->smtp_from\" <$this->smtp_username>\r\n";
        $header .= "Reply-To: $this->smtp_username\r\n";
        $header .= "Content-Type: text/$type; charset=\"$this->smtp_charset\"\r\n";
        
        $this->headers = $header;
	}
}